package src 
{
	import flash.display.Sprite;
	/**
	 * ...
	 * @author Felipe Soares Pinhel
	 */
	public class Level extends Sprite
	{
		
		private var hud : Hud;
		private var gameState : String;
		public var level_01 : Level01;
		public var player :  Player;
		private var controller : Controller;
		
		
		private var moveBackg : Boolean = false;
		private var movePlayer : Boolean = true;
		private var changeController : Boolean = false;
		
		public var space				:   Boolean;
		private var playerJumping		:	Boolean;
		private var jumpSpeedLimit		:	int;
		private var jumpSpeed			:	Number ;
		
		public function Level() 
		{
			init();			
		}
		
		private function init() : void
		{
			
			MediaPlayer.soundChannel.stop();
			MediaPlayer.level();
			
			gameState = "LEVEL_01";
			levelManager();
			newHud();
			
			Main._instance.stage.focus = this;
			
			jumpSpeedLimit = 15;
			jumpSpeed = jumpSpeedLimit;
			
		}
		private function newHud() : void
		{
			hud = new Hud();
			this.addChild(hud);
		}
		private function levelManager() : void
		{
			switch(gameState)
			{
				case "LEVEL_01" :					
					
					level_01 = new Level01();
					this.addChild(level_01);
					
					player = new Player();
					player.x = 50;
					player.y = 100;
					this.addChild(player);
					
					controller = new Controller(player.x, player.y);					
				
				break;
				
			}
		}		
		
		private function trash() : void
		{
			if (level_01 != null)
			{
				this.addChild(level_01);
				level_01 = null;
			}
			
		}
		public function update()
		{
			if ( hud != null)
			{
				hud.update();
			}
			
			if(movePlayer)
			{
				/*if(changeController)
				{
					player.y = 200;
					
					controller.x = Main._instance.stage.stageWidth / 2;
					controller.y = 100;
					controller.speedX = 3;
					changeController = false;
				}*/
				
				//oi?
				player.x = controller.x;
				player.y = controller.y;
				
				//cai com a gravidade, DEPENDE DOS dois!!!!!!!!!!!!!!, porque?????
				player.speedY += controller.speedY;
				player.update();				
				
				//faz ele mover esquerda direita
				controller.update();				
				
			}
			
			
			var chegueiNoFim : Boolean = false;
			
			if ( player.x > Main._instance.stage.stageWidth / 2 && !chegueiNoFim)
			{
				movePlayer = false;
				moveBackg = true;
			}
			
			if ( moveBackg)
			{		
				//entra uma vez só para ajustar a posicao do background no canto superior esquerdo
				/*if(!changeController)
				{					
					controller = new Controller(level_01.backg.x, level_01.backg.y);
					controller.speedX = -3;
					changeController = true;
				}*/
				
				controller.speedX = -3;
				
				level_01.backg.x = controller.x - Main._instance.stage.stageWidth / 2 - player.width/2;
				level_01.backg.y = controller.y - 100;
				
				//level_01.backg.speedX -= controller.speedX;
				
				level_01.brick.x = level_01.backg.x + 550;
				level_01.brick.y = Main._instance.stage.stageHeight - level_01.floor.height - 40;
				
				level_01.boss.x = level_01.backg.x + 1500;
				level_01.boss.y = Main._instance.stage.stageHeight - level_01.floor.height - 40;
				
				controller.update();
				
				trace("passei a mover");
				
			}
						
			if ( level_01.backg.x <= -level_01.backg.width + 550)
			{
				movePlayer = true;
				moveBackg = false;
				
				player.x = Main._instance.stage.stageWidth / 2;
				
				chegueiNoFim = true;
				
				//controller.x = player.x;
				//player.y = controller.y;
				
			}
			
			trace("level_01.backg.x " + level_01.backg.x);
			
			//if (level_01.backg.x < -55)
			//{
				//level_01.backg.x = -55;				
			//}
			
			
			/*
			if (player.x <=  Main._instance.stage.stageWidth / 2)
			{
				movePlayer = true;
				moveBackg = false;					
			}
			
			else if ( level_01.backg.x <= -level_01.backg.width + 550)
			{
				movePlayer = true;
				moveBackg = false;
			}
			
			*/
			
			//Mantém o player no chão
			if ( player.y >= 320)
			{
				
				player.y = 320;
			}
			
			//Não deixa o personagem sair pela a esquerda
			if (player.x-player.width/2 < 0)
			{
				player.x = (player.width/2)+1;
			}
			//Não permite o personagem sair pela direita
			if (player.x > Main._instance.stage.stageWidth - player.width/2)
			{
				player.x = Main._instance.stage.stageWidth - player.width / 2 -1;
			}
			
			//Pulo
			space = controller.space;			
			if ( space )
			{
				playerJump();
			}
		}
		
		
		public function playerJump()	: void
		{
				if (!playerJumping)
				{
					
					jumpSpeed = jumpSpeedLimit * -1;
					player.y += jumpSpeed;
					playerJumping = true;
					
				} 
				else 
				{
					if (jumpSpeed < 0)
					{
						
						jumpSpeed *= 1 - jumpSpeedLimit / 90;
						
						if (jumpSpeed > -jumpSpeedLimit / 5)
						{
							jumpSpeed *= -1;
						}
					}
					if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
					{
						jumpSpeed *= 1 + jumpSpeedLimit/50;
					}
					player.y += jumpSpeed;

					if (player.y >=  320)
					{
						playerJumping = false;
						player.y =  320 ;
						space = false;
						
						controller.space =  space;
						 
					}
				}
			}
		
		
		
		
		
	}

}